var Web = {};

Web = {        
    baseUrl: function(url){
        var a = location.href;
        var be = (a.split('/')).filter(function(n){return n;});
        var baseUrl = (location.protocol + '//' + location.host + '/' + be[2] + '/' + be[3] + '/' + url).trim();
        
        return baseUrl;
    },
            
    go: function(el) {
        $(el).hide();
        $('.tbl-container').show();
    },
                
    Do: function(a) {
        "use strict";                
        a = a || {};
        var param = {};
        var process = true;
        var url = $('#url').val() || '';
        var modID = { resource : 'resID', user: 'userID' };
        var controller = url.split('/')[1];                       
        var surl;

        $(a.formid).submit(function(e) {
            e.preventDefault();
        });
        
        if (a.param.type === 'delete') {
            process = window.confirm('Delete this row ?');
            if (process !== false) $('.my-overlay').show();
            surl = url + '/delete/id/' + a.param.id;
            
        } else if (a.param.type === 'edit') {
            $('.my-overlay').show();
            location.href = this.baseUrl( url ) + '/edit/id/' + a.param.id;
            
            return false;
            
        } else if (a.param.type === 'edit-param') {
            $('.my-overlay').show();
            location.href = this.baseUrl( a.param.url ) + '/resid/' + a.param.id;
            
            return false;
            
        } else if (a.param.type === 'add') {
            $('.my-overlay').show(); //$('.tbl-container').hide();
            
            location.href = this.baseUrl( url ) + '/add' + ('undefined' !== typeof a.param.id ? '/resid/' + a.param.id : '' );
            return false;
            
        } else if (a.param.type === 'save') {
            surl = url + '/save';
                        
            var data = $(a.formid).serializeArray();                        
            $.each(data, function(key, item) { param[ item.name ] = item.value; });
            
            $('.my-overlay').show();
        } else if (a.param.type === 'cancel') {
            $('.my-overlay').show();
            location.href = this.baseUrl( url ) + ('undefined' !== typeof a.param.id ? '/index/resid/' + a.param.id : '' );
            return false;
        }
        
        if (process !== false)
            $.ajax({
                url: this.baseUrl( surl ),
                type: 'POST',
                data: param,
                dataType: 'JSON',
                complete: function(response) {
                    response = JSON.parse(response.responseText);
                    
                    var id = modID[controller];

                    if (a.param.type === 'save') {
                        alert(response.status);
                        location.href = Web.baseUrl( url ) + ('undefined' !== typeof a.param.id ? '/index/resid/' + a.param.id : '' );
                    }

                    else if (a.param.type === 'delete') {
                        alert(response.status);
                        Web.loadPage();
                    }
                    
                    $('.my-overlay').hide();
                }
            });                        
    },
    Search: function() {
        Web.loadPage();
    },
    loadPage: function(page) {       
        var q = $('#_search').val();
        var url = $('#url').val();
        var resid = $('#resid').val();
        
        $('.my-overlay').show();
        $.ajax({
            url: this.baseUrl( url ) + ('undefined' !== typeof resid ? '/index/resid/' + resid : '' ),
            data: {
                q: q || '',
                page: page || 1
            },
            type: 'POST',
            dataType: 'JSON',
            complete: function(response) {
                response = JSON.parse(response.responseText);
                
                if (typeof response.data === 'object') {
                
                var modID = { 
                    resource : 'resID', 
                    user: 'userID', 
                    group: 'groupID', 
                    'resource-parameter' : 'parID',
                    'menu' : 'menuID',
                    acl: 'groupID',
                    category: 'catID'
                };
                
                var splt = url.split('/')[1];
                var id = modID[splt];
                var list;                    
                    $('tbody').html('');
                    $.each(response.data, function(no, item) {
                        $('table.index').find('tbody').append(
                                '<tr class="' + (($('tr:last')[0].rowIndex % 2 === 0) ? 'first' : 'alt') + '">\n\
                                 <td style="text-align: center">' + eval(response.offset + 1) + '</td>' +
                                '<td>' + (null === item.name ? '-' : item.name) + '</td>' +
                                '<td>' + (null === item[ response.fieldname ] ? '-' : item[ response.fieldname ]) + '</td>' +
                                '<td>' + (null === item.create_by ? '-' : item.create_by) + '</td>' +
                                '<td>' + (null === item.create_on ? '-' : item.create_on) + '</td>' +
                                '<td>' + (null === item.update_by ? '-' : item.update_by) + '</td>' +
                                '<td> ' + (null === item.update_on ? '-' : item.update_on) + '</td>' +
                                '<td style="text-align: center">'
                                    +
                                        (splt !== 'acl' ? '<a href="javascript:Web.Do({param: {type: \'delete\', id: '+item[id]+'}})" class="icon-remove" title="Delete"></a>' + ' | \n' : '')
                                    +
                                        '<a href="javascript:Web.Do({param: {type: \'edit\', id: '+item[id]+ (splt === 'resource-parameter' ? ', resid: ' + $('#resid').val() : '') +'}})" class="icon-edit" title="Edit"></a>'
                                    +
                                        (splt === 'resource' ? ' | <a href="javascript:Web.Do({param: {type: \'edit-param\', id: '+ item[id] +', url: \'admin/resource-parameter/index\'}})" class="icon-pushpin" title="Edit Parameter"></a>' : '')
                                    +
                            '</td></tr>');
                        
                        response.offset += 1;
                    });
                    
                    $('.search_pg').html(response.pview);
                } else if(response.errorplus) {
                    alert(response.errorplus);
                    location.href = response.url;
                } else {
                    $('tbody').html('<tr><td colspan="8" style="text-align: center; border: none">' + response.error + '</td></tr>');
                    $('.search_pg').html('&nbsp;');
                }
                Web.go('.divupdate');
                $('.my-overlay').hide();
            }
        });
    },
    
    loadList: function(id) {
        $('#groupmenu').bind('change', function() {
            
            if ('' === this.value) return false;
            
            $.ajax({
                url: Web.baseUrl('admin/menu/look-up-parent-menu'),
                data: {
                    gmenuID : this.value
                },
                type: "POST",
                dataType: "JSON",
                complete: function(response) {
                    response = JSON.parse(response.responseText);
                    $('#parentmenu').html(response.menuparent);
                }
            });
        });        
    },
    
    submitForm: function(a) {
        var param = {};
        
        $(a.formid).submit(function(e) {
            e.preventDefault();
        });
        
        var data = $(a.formid).serializeArray();                        
        $.each(data, function(key, item) { param[ item.name ] = item.value; });
        
        $.ajax({
            url: Web.baseUrl( a.param.url ),
            data: param,
            type: 'POST',
            dataType: 'JSON',
            complete: function(response) {
                response = JSON.parse(response.responseText);
                
                $('div.notice')
                        .html(response.message)
                        .show('fast')
                        .delay(4000)
                        .hide('fast');
            }
        });
    }
};